package com.jingbian.study.controller;

import com.jingbian.study.entity.InstallJobTO;
import com.jingbian.study.entity.Jobs;
import com.jingbian.study.entity.UnloadJobTO;
import com.jingbian.study.resp.ResultData;
import com.jingbian.study.service.IJobService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import lombok.Setter;
import org.quartz.*;
import org.quartz.impl.StdSchedulerFactory;
import groovy.lang.GroovyClassLoader;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.List;
import java.util.Map;

@Setter
@RestController
public class JobController {

    private IJobService jobService;
    private Scheduler scheduler;



    @Autowired
    public JobController(IJobService jobService,Scheduler scheduler){
        this.jobService=jobService;
        this.scheduler=scheduler;
    }
    @PostMapping("/installJob")
    @Tag(name = "Groovy")
    @Operation(description = "安装任务")
    public ResultData<String> installJob(@RequestParam Integer jobId,@RequestParam String triggerName,@RequestParam String triggerGroup) throws SchedulerException, IOException, ClassNotFoundException {
        String info= jobService.installJob(jobId,triggerName,triggerGroup);
        return ResultData.success(info);
    }
//    @PostMapping("/installJob")
//    @Tag(name = "Groovy")
//    @Operation(description = "安装任务")
//    public ResultData<String> installJob(@RequestBody InstallJobTO installJobTO) throws SchedulerException, IOException, ClassNotFoundException {
//        try {
//
//            Class<? extends Job> clazz = classLoader.parseClass(installJobTO.getCode());
//            // 确保动态加载的类实现了 Job 接口
//            if (!Job.class.isAssignableFrom(clazz)) {
//                return ResultData.fail("500","提供的脚本没有实现 Job 接口。");
//            }
//
//            // 将类转换为 Class<? extends Job>
////            Class<? extends Job> jobClass = (Class<? extends Job>) clazz;
//
//            // 使用动态加载的类创建 JobDetail
//            JobDetail job = JobBuilder.newJob(clazz)
//                    .withIdentity(installJobTO.getJobName(), installJobTO.getJobGroup())
//                    .build();
//
//            // 定义一个每 10 秒触发一次的触发器
//            Trigger trigger = TriggerBuilder.newTrigger()
//                    .withIdentity(installJobTO.getTriggerName(), installJobTO.getJobGroup())
//                    .startNow()
//                    .withSchedule(SimpleScheduleBuilder.simpleSchedule()
//                            .withIntervalInSeconds(10)
//                            .repeatForever())
//                    .build();
//
//            // 使用调度器调度任务
//            scheduler.scheduleJob(job, trigger);
//
//            return ResultData.success("任务启动成功。");
//        }catch (RuntimeException e){
//            System.out.println(e);
//            return ResultData.fail("500","任务启动失败,是否替换任务。");
//        }
//    }

    @PostMapping("/unloadJob")
    @Tag(name = "Groovy")
    @Operation(description = "卸载任务")
    public String unloadJob(@RequestParam Integer jobId,@RequestParam String triggerName,@RequestParam String triggerGroup) throws SchedulerException {

        jobService.unloadJob(jobId,triggerName,triggerGroup);


        return "任务删除成功。";
    }

    @PostMapping("/getJobTemplate")
    @Tag(name = "Groovy")
    @Operation(description = "给出任务模板")
    public String getJobTemplate(){
        String template="import org.quartz.Job;\n" +
                "import org.quartz.JobExecutionContext;\n" +
                "\n" +
                "public class JobTemplate implements Job {\n" +
                "\n" +
                "    public void execute(JobExecutionContext jobExecutionContext){\n" +
                "\n" +
                "    }\n" +
                "}";

        return template;
    }














































    @Tag(name = "基础增删改查Job")
    @PostMapping("/addJob")
    @Schema(description = "保存任务")
    public ResultData<String> addJob(@RequestBody Jobs jobs){
        System.out.println(jobs);
        try {
            jobService.saveJob(jobs);
        }catch(RuntimeException e) {
            return ResultData.fail("500",e.getMessage());
        }

        return ResultData.success("保存成功");
    }

    @Tag(name = "基础增删改查Job")
    @PostMapping("/deleteJob")
    @Schema(description = "删除任务")
    public ResultData<String> deleteJob(@RequestParam Integer id ){
        try {
            jobService.deleteJob(id);
        }catch (RuntimeException e){
            return ResultData.fail("500",e.getMessage());
        }
        return ResultData.success("删除成功");
    }

    @Tag(name = "基础增删改查Job")
    @PostMapping("/updateJob")
    @Schema(description = "更新任务")
    public ResultData<String> updateJob(@RequestBody Jobs jobs){
        try {
            jobService.updateJob(jobs);
        }catch (RuntimeException e){
            return ResultData.fail("500",e.getMessage());
        }

        return ResultData.success("更新成功");
    }

    @Tag(name="基础增删改查Job")
    @Schema(description = "获取所有任务")
    @PostMapping("/getJobList")
    public ResultData<List<Jobs>> gitJobList(){
        try {
            List<Jobs> jobsList = jobService.gitJobList();
            return ResultData.success(jobsList);
        }catch (RuntimeException e){
            return ResultData.fail("500",e.getMessage());
        }

    }

    @Tag(name="基础增删改查Job")
    @Schema(description = "获取单个任务")
    @PostMapping("/getJob")
    public ResultData<Jobs> getJob(@RequestParam Integer id){
        try {
            Jobs jobs= jobService.getJob(id);
            return ResultData.success(jobs);
        }catch (RuntimeException e){
            return ResultData.fail("500",e.getMessage());
        }

    }


}